################################################################################
# Board: MKS Robin Nano v1.1 (Flying Bear Reborn 2.0) / MKS Robin Nano v1.3 / MKS Robin Nano-S v1.3
# Printer: Flying Bear Ghost 4S / Ghost 5 / Reborn
# G-code Macros by ODUCCEU
################################################################################
# WARNING! DO NOT EDIT THIS FILE!
# To override settings from this file, you can copy and paste the relevant
# sections into your printer.cfg and change it there.
################################################################################

# Print Job Management #########################################################

[gcode_macro START_PRINT]
## PrusaSliser/SuperSlicer: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={first_layer_temperature[0]} BED_TEMP={first_layer_bed_temperature[0]}
## Cura: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={material_print_temperature_layer_0} BED_TEMP={material_bed_temperature_layer_0}
description: Start routine for the print
variable_retract: 2
variable_printingstate: 0
gcode:
    {% set extruder_temp = params.EXTRUDER_TEMP|default(240)|float %}
    {% set bed_temp = params.BED_TEMP|default(70)|float %}
    {% set mesh_profile = params.MESH_PROFILE|default('default') %}
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% if bed_temp > 90 %}
        {% set bed_temp_preheat = (bed_temp * 0.95)|int %}
    {% else %}
        {% set bed_temp_preheat = (bed_temp * 0.90)|int %}
    {% endif %}
    CLEAR_PAUSE
    M220 S100                                                                   # reset feedrate
    M221 S100                                                                   # reset flowrate
    G90                                                                         # absolute positioning
    M83                                                                         # relative extrusion mode
    BED_MESH_PROFILE LOAD={mesh_profile}
	M117 Heating bed, homing and tilting...
	RESPOND MSG="Heating bed, homing and tilting..."
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={bed_temp}                  # set bed temp
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={bed_temp_preheat}               # wait until bed is partially heated
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET=160               # set extruder temp
	MAYBE_HOME
    SET_VELOCITY_LIMIT ACCEL=5000 ACCEL_TO_DECEL=2500
    Z_TILT_ADJUST
    #_START_PRINT_PARK
	M117 Heating Extruder and loading the filament...
	RESPOND MSG="Heating Extruder and loading the filament..."
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET={extruder_temp}               # set extruder temp
    # Go to prime nozzle
    #G0 Z10 Y450 F30000
    #G0 X490 F30000
    #G0 Y490 F30000
    G0 X490 Y497 Z10 F30000
    
    #TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={bed_temp}                       # wait for bed temp
    #TEMPERATURE_WAIT SENSOR=extruder MINIMUM={extruder_temp}                    # wait for extruder temp
    #G1 E{E} F600 # F2100                                                               # prime
    #G0 X1 Y3 F12000                                                                 # raise Z
    #G1 E-0.5 F600
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=printingstate VALUE=1
    LOAD_FILAMENT T={extruder_temp} MOVECLEARAFTER=1

[gcode_macro END_PRINT]
gcode:
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=printingstate VALUE=0
    #{% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% set E = 1 %}
    TURN_OFF_HEATERS
    M107                                                                        # turn off fan
    G91                                                                         # relative positioning
    G1 E-{E} F2100                                                              # retract
    G0 X1 Y1 F6000                                                              # wipe
    G0 Z2 F600                                                                  # raise Z
    G90                                                                         # absolute positioning
    _END_PRINT_PARK
    #PARK Z=80 Y=50
    #BEEP P=200 S=250
    UNLOAD_FILAMENT T=0
    M84                                                                         # turn off all motors
    SAVE_UPDATES
    ACTIVATE_POWER_OFF

[gcode_macro PARK]
gcode:
    {% set x_park = params.X|default(490)|float %} # printer.toolhead.axis_minimum.x # 250
    {% set y_park = params.Y|default(497)|float %} # printer.toolhead.axis_minimum.y # 450
    {% set z_park = params.Z|default(20)|float + printer.toolhead.position.z|float %}
    {% set x_max = printer.toolhead.axis_maximum.x|float %}
    {% set y_max = printer.toolhead.axis_maximum.y|float %}
    {% set z_max = printer.toolhead.axis_maximum.z|float %}
    {% if x_park > x_max %}
        {% set x_park = x_max %}
    {% endif %}
    {% if y_park > y_max %}
        {% set y_park = y_max %}
    {% endif %}
    {% if z_park > z_max %}
        {% set z_park = z_max %}
    {% endif %}
    #{% if printer.toolhead.homed_axes != "xyz" %} G28 {% endif %}               # home if not homed
    {% if printer.toolhead.homed_axes == "xyz" %}
        SAVE_GCODE_STATE NAME=PARK_STATE
        G90                                                                         # absolute positioning
        G0 Z{z_park} F600 
        G0 X{x_park} Y{y_park} F30000
        RESTORE_GCODE_STATE name=PARK_STATE
    {% endif %}

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: BASE_PAUSE
variable_extrudertemp: 1
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=printingstate VALUE=0
    SAVE_GCODE_STATE NAME=PAUSE_STATE
    SET_IDLE_TIMEOUT TIMEOUT=172800
    BASE_PAUSE
    {% if printer.extruder.can_extrude|lower == 'true' %}
        G91
        G1 E-{E} F2100                                                          # retract
        G90
    {% else %}
        {action_respond_info("Extruder is not hot enough")}
    {% endif %}
    # turn off extruder heater
    #{% set printer["gcode_macro PAUSE"].extrudertemp = printer.extruder.target %}
    SET_GCODE_VARIABLE MACRO=PAUSE VARIABLE=extrudertemp VALUE={ printer.extruder.target }
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET=0
    PARK X=490 Y=497

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: BASE_RESUME
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% if 'VELOCITY' in params|upper %}
        {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
    {%else %}
        {% set get_params = "" %}
    {% endif %}

    SET_HEATER_TEMPERATURE HEATER=extruder TARGET={printer["gcode_macro PAUSE"].extrudertemp}
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={printer["gcode_macro PAUSE"].extrudertemp - 5}
    
    #{% if printer.extruder.can_extrude|lower == 'true' %}
    #G91
    #G90
    #{% else %}
    #    {action_respond_info("Extruder is not hot enough")}
    #{% endif %}  
    RESTORE_GCODE_STATE NAME=PAUSE_STATE MOVE=1
    G1 E{E} F2100
    BASE_RESUME {get_params}
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=printingstate VALUE=1

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: BASE_CANCEL_PRINT
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=printingstate VALUE=0
    CLEAR_PAUSE
    TURN_OFF_HEATERS
    M107                                                                        # turn off fan
    {% if printer.extruder.can_extrude|lower == 'true' %}
        G91                                                                     # relative positioning
        G1 E-{E} F2100                                                          # retract
        G0 Z2 F600                                                              # raise Z
        G90                                                                     # absolute positioning
    {% else %}
        {action_respond_info("Extruder is not hot enough")}
    {% endif %}
    SDCARD_RESET_FILE
    BASE_CANCEL_PRINT
    PARK Z=30
    M84                                                                         # turn off all motors

# Save to Config ##########################################################

[gcode_macro SAVE_UPDATES]
description: Run SAVE_CONFIG if there are updates that it may persist to disk
gcode:
    {% if printer.configfile.save_config_pending %}
        M118 Saving and restarting now
        G4 P2000                                                                # wait 2 seconds
        SAVE_CONFIG
    {% endif %}

################################################################################
